package com.salton123.imagepicker.adapter;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.core.content.FileProvider;
import androidx.viewpager.widget.PagerAdapter;

import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.github.chrisbanes.photoview.PhotoView;
import com.salton123.imagepciker.R;
import com.salton123.imagepicker.ImagePicker;
import com.salton123.imagepicker.bean.ImageItem;
import com.salton123.imagepicker.log.ILog;
import com.salton123.imagepicker.util.ImageUtils;
import com.salton123.imagepicker.util.ProviderUtil;
import com.salton123.imagepicker.util.Utils;

import java.io.File;
import java.util.ArrayList;

/**
 * ================================================
 * 作    者：jeasonlzy（廖子尧 Github地址：https://github.com/jeasonlzy0216
 * 版    本：1.0
 * 创建日期：2016/5/19
 * 描    述：
 * 修订历史：
 * ================================================
 */
public class ImagePageAdapter extends PagerAdapter {

    private static final String TAG = "ImagePageAdapter";

    private int screenWidth;
    private int screenHeight;
    private ArrayList<ImageItem> images = new ArrayList<>();
    private Activity mActivity;
    public PhotoViewClickListener listener;
    private ImagePicker imagePicker;

    public ImagePageAdapter(Activity activity, ArrayList<ImageItem> images) {
        this.mActivity = activity;
        this.images = images;
        this.imagePicker = ImagePicker.getInstance();

        DisplayMetrics dm = Utils.getScreenPix(activity);
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
    }

    public void setData(ArrayList<ImageItem> images) {
        this.images = images;
    }

    public void setPhotoViewClickListener(PhotoViewClickListener listener) {
        this.listener = listener;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View temView = View.inflate(container.getContext(), R.layout.adapter_image_page, null);
        final PhotoView photoView = temView.findViewById(R.id.pv_photoview);
        final ImageView ivPlayer = temView.findViewById(R.id.iv_player);
        final ImageItem imageItem = images.get(position);
        if (imagePicker.getImageLoader() != null) {
            imagePicker.getImageLoader().loadImage(photoView, imageItem.path,
                    screenWidth, screenHeight, R.drawable.ic_default_image);
        } else {
            ImageUtils.loadImage(photoView, imageItem.path, screenWidth, screenHeight, R.drawable.ic_default_image);
        }
        photoView.setOnPhotoTapListener(new OnPhotoTapListener() {
            @Override
            public void onPhotoTap(ImageView view, float x, float y) {
                if (listener != null) listener.onPhotoTapListener(view, x, y);
            }
        });
        ivPlayer.setVisibility(imageItem.mimeType.startsWith("video") ? View.VISIBLE : View.GONE);
        ivPlayer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    File file = new File(imageItem.path);
                    Uri uri;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        Uri contentUri = FileProvider.getUriForFile(mActivity,
                                ProviderUtil.getFileProviderName(mActivity), file);
                        intent.setDataAndType(contentUri, "video/*");
                    } else {
                        uri = Uri.fromFile(file);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setDataAndType(uri, "video/*");
                    }
                    mActivity.startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                    ILog.e(TAG, "ivPlayer click error: " + e.getMessage());
                }
            }
        });
        container.addView(temView);
        return temView;
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    public interface PhotoViewClickListener {
        void onPhotoTapListener(View view, float v, float v1);
    }
}
